#region License, Terms and Conditions
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
#endregion
using System;
using System.Web;
using System.Web.Mvc;
using raya.Library;
using raya.Models.applications;
using raya.Models.people;

namespace raya.Controllers
{
    public class appsController : baseController
    {
        public ActionResult Index()
        {
            return Redirect("/home?tab=applications");
        }
        public ActionResult Active(string id)
        {
            if (Session["id"] == null)
            {
                return Content("");
            }
            var data = GetProfile(id ?? Session["id"].ToString());
            return View("apps_active", data);
        }

        // remove app from gallery
        public void Delete(string id)
        {
            if (Session["id"] == null || Session["id"].ToString() != Utility.ADMINID)
            {
                return;
            }
            var apps = new applications();
            apps.remove_application_permanent(id);
            apps.Dispose();
        }
        public ActionResult Manage()
        {
            if (Session["id"] == null)
            {
                return Content("");
            }
            var data = GetProfile(Session["id"].ToString());
            return View("apps_manage", data);
        }

        public ActionResult Gallery()
        {
            var apps = new applications();
            var data = apps.get_all_applications();
            return View("apps_gallery", data);
        }

        public ActionResult New()
        {
            if (Session["id"] == null || 
                Request.QueryString["appUrl"] == null)
            {
                handleErrors("missing parameters");
                return Redirect("/");
            }
            string url = HttpUtility.UrlDecode(Request.QueryString["appUrl"]).Trim();
            if (url.Length <= "http://".Length ||
                !url.StartsWith("http://"))
            {
                handleErrors("invalid gadget URL");
                return Redirect("/");
            }
            var apps = new applications();
            
#if AZURE
            string sessionid = Session["id"].ToString();
            ApplicationAdded ret = apps.add_application(sessionid, url, Request.QueryString["newappTypeCB"] == "on" ? applications.appType.raya : applications.appType.external);
#else
            int sessionid = int.Parse(Session["id"].ToString());
            ApplicationAdded ret = apps.add_application(sessionid, url, Request.QueryString["newappTypeCB"]=="on"?applications.appType.raya:applications.appType.external);
#endif
            if (!string.IsNullOrEmpty(ret.app_id) && !string.IsNullOrEmpty(ret.mod_id) && String.IsNullOrEmpty(ret.error))
            {
                // App added ok, goto app settings
                return RedirectToAction("application", "profile", new { pid = sessionid, aid = ret.app_id, mid = ret.mod_id });
            }

            // Using the home controller to display the error on the person"s home page
            handleErrors("Could not add application: " + ret.error); 
            
            apps.Dispose();
            return RedirectToAction("Index");
        }

        public ActionResult Preview(string id)
        {
            if (Session["id"] == null)
            {
                return Content("");
            }
            people people = new people();
            var person = people.GetPerson(Session["id"].ToString(), true);
            applications apps = new applications();
#if AZURE
            Application application = apps.get_application_by_id(id);
#else
            Application application = apps.get_application_by_id(int.Parse(id));
#endif
            ApplicationCanvas data = new ApplicationCanvas();
            ProfileApplication personapp = new ProfileApplication { app = application };
            data.application = personapp;
            data.person = person;
            data.is_owner = true;
            people.Dispose();
            apps.Dispose();
            return View("apps_preview", data);
        }

        public ActionResult Settings()
        {
            return Content("Coming soon");
        }

        // remove app from person only
        public ActionResult Remove(string aid, string mid)
        {
            if (Session["id"] == null)
            {
                return Redirect("/");
            }
            var apps = new applications();
#if AZURE
            string message = apps.remove_application(Session["id"].ToString(), aid, mid) ? "Application removed" : "Could not remove application, invalid id";
            
#else
            string message = apps.remove_application(int.Parse(Session["id"].ToString()), int.Parse(aid), int.Parse(mid)) ? "Application removed" : "Could not remove application, invalid id";
#endif
            Session["message"] = message;
            apps.Dispose();
            return RedirectToAction("Index");
        }
    }
}